package com.cssy.authority.dao.impl;

import com.cssy.authority.dao.UserDAO;
import com.cssy.authority.entity.User;
import com.cssy.authority.entity.UserExample;
import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class UserDAOImpl implements UserDAO {
    private SqlMapClient sqlMapClient;

    public UserDAOImpl(SqlMapClient sqlMapClient) {
        super();
        this.sqlMapClient = sqlMapClient;
    }

    public void insert(User record) throws SQLException {
        sqlMapClient.insert("sc_user.abatorgenerated_insert", record);
    }

    public int updateByPrimaryKey(User record) throws SQLException {
        int rows = sqlMapClient.update("sc_user.abatorgenerated_updateByPrimaryKey", record);
        return rows;
    }

    public int updateByPrimaryKeySelective(User record) throws SQLException {
        int rows = sqlMapClient.update("sc_user.abatorgenerated_updateByPrimaryKeySelective", record);
        return rows;
    }

    public List selectByExample(UserExample example) throws SQLException {
        List list = sqlMapClient.queryForList("sc_user.abatorgenerated_selectByExample", example);
        return list;
    }

    public User selectByPrimaryKey(Long id) throws SQLException {
        User key = new User();
        key.setId(id);
        User record = (User) sqlMapClient.queryForObject("sc_user.abatorgenerated_selectByPrimaryKey", key);
        return record;
    }

    public int deleteByExample(UserExample example) throws SQLException {
        int rows = sqlMapClient.delete("sc_user.abatorgenerated_deleteByExample", example);
        return rows;
    }

    public int deleteByPrimaryKey(Long id) throws SQLException {
        User key = new User();
        key.setId(id);
        int rows = sqlMapClient.delete("sc_user.abatorgenerated_deleteByPrimaryKey", key);
        return rows;
    }

    public int countByExample(UserExample example) throws SQLException {
        Integer count = (Integer)  sqlMapClient.queryForObject("sc_user.abatorgenerated_countByExample", example);
        return count.intValue();
    }

    public int updateByExampleSelective(User record, UserExample example) throws SQLException {
        UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
        int rows = sqlMapClient.update("sc_user.abatorgenerated_updateByExampleSelective", parms);
        return rows;
    }

    public int updateByExample(User record, UserExample example) throws SQLException {
        UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
        int rows = sqlMapClient.update("sc_user.abatorgenerated_updateByExample", parms);
        return rows;
    }

    private static class UpdateByExampleParms extends UserExample {
        private Object record;

        public UpdateByExampleParms(Object record, UserExample example) {
            super(example);
            this.record = record;
        }

        public Object getRecord() {
            return record;
        }
    }
    @Override
	public List getUserCondtion(Map map) throws SQLException {
		List list = sqlMapClient.queryForList("sc_user.getUserCondtion", map);
		return list;
	}

	@Override
	public int getSelectCount(Map map) throws SQLException {
		int count = (Integer) sqlMapClient.queryForObject("sc_user.getSelectUserCount", map);
		return count;
	}

	@Override
	public List getSelectUser(Map map) throws SQLException {
		List list = sqlMapClient.queryForList("sc_user.getSelectUser", map);
		return list;
	}

	

	@Override
	public User getByUserName(String name) throws SQLException {
		User user = (User) sqlMapClient.queryForObject("sc_user.selectByUserName", name);
		return user;
	}

	@Override
	public Date getEnterDate(Long userId) throws SQLException {
		Date enterDate = (Date) sqlMapClient.queryForObject("sc_user.selectCheckin", userId);
		return enterDate;
	}

	@Override
	public String getUserStaffId(String userId) throws SQLException {
		String staffId = (String) sqlMapClient.queryForObject("sc_user.selectStaffId", userId);
		return staffId;
	}

	@Override
	public List<User> getBirthdayUser(Date date) throws SQLException {
		List<User> users = sqlMapClient.queryForList("sc_user.selectBirthDayUser", date);
		return users;
	}

	@Override
	public List<User> getContractExpUser(Date date) throws SQLException {
		List<User> users = sqlMapClient.queryForList("sc_user.selectContractExpUser", date);
		return users;
	}

	@Override
	public List<User> getToPostiveUser(Date date) throws SQLException {
		List<User> users = sqlMapClient.queryForList("sc_user.selectToPositiveUser", date);
		return users;
	}

	@Override
	public void deleteUser(int userId) throws SQLException {
		sqlMapClient.update("sc_user.deleteUser", userId);
	}

	@Override
	public int updatePassword(Map map) throws SQLException {
		int count = sqlMapClient.update("sc_user.updatePassword", map);
		return count;
	}

	

	@Override
	public int getSelectCountNoRole(Map map) throws SQLException {
		int count = (Integer) sqlMapClient.queryForObject("sc_user.getSelectUserCountNoRole", map);
		return count;
	}

	@Override
	public List getSelectUserNoRole(Map map) throws SQLException {
		List list = sqlMapClient.queryForList("sc_user.getSelectUserNoRole", map);
		return list;
	}
}